<script>  
    function show_u(param,dur){
        $('#multiselect_u').animate({height: param+'px'},dur);
        
        $('#button2').html('Skryť možnosti');   
    }    

    function hide_u(param){
        $('#multiselect_u').animate({height: param+'px'},300);
                
        $('#button2').html('Zobraziť možnosti'); 
    } 
    
    function show_d(param,dur){
        $('#multiselect_d').animate({height: param+'px'},dur);
         
        $('#button').html('Skryť možnosti');   
    }    

    function hide_d(param){
        $('#multiselect_d').animate({height: param+'px'},300);
                
        $('#button').html('Zobraziť možnosti'); 
    }

    
    $(document).ready(
    function(){

    
        $('#button').click(
        function(){
            $('someElement').hide().animate({height:'20px'});
                    
           
            if ($('#button').html() != 'Skryť možnosti') {
                show_d(200,300); 
            }
            else {
                hide_d(20);
            }
            
        }
    )
        
        $('#button2').click(
        function(){
            $('someElement').hide().animate({height:'20px'});
                    
           
            if ($('#button2').html() != 'Skryť možnosti') {
                show_u(200,300);
            }
            else {
                hide_u(20);
            }
            
        }
    )
            
        $('#zobraz_table').show();
        hide_u(20);
        hide_d(20);
        
        var $isPrekryv = false;
        for ($i = 1; $i < 6; $i++) {
            $(".coll" + $i).each(function() {
                var $lastTop = 0;
                var $lastHeight = 0;
                var $curTop = 0;
                var $curHeight = 0;

                var $lastBottom = 0;
                var $prekryvEnd = 0;
                $(this).children("a").each(function() {
                    $(this).children('.cell').each(function() {
                        $curTop = parseInt($(this).css('top'));
                        $curHeight = parseInt($(this).css('height'));

                        $lastBottom = ($lastTop + $lastHeight);
                        $prekryvEnd = $lastBottom - $curTop;

                        if ($lastBottom > $curTop) {
                            $(this).prepend('<div class="prekryv" style="top: ' + 0 + 'px; height: ' + $prekryvEnd + 'px;"></div>');
                            $isPrekryv = true;
                        }

                        $lastTop = $curTop;
                        $lastHeight = $curHeight;
                    });
                });
            });

        }
        
        if($isPrekryv) {
            $(".color-prekryv").show();
        }
    }
);
</script>

<div id="calendar_view">

    <div id="calendar">
        <div id="first_coll">
            <div id="blank"></div>
            <?php for ($i = 0; $i <= 13; $i++) { ?> <div class="row_head"><div class="hours"><?= ($i + 8) . ":00" ?></div></div> <?php }; ?>
        </div>

        <?php
        $colors = array('#e9967a', '#ffa500', '#ff1493', '#ba55d3', '#8a2be2', '#b8860b',
            '#009999', '#32cd32', '#6b8e23', 'blue');
        $j = 0;
        $user_color = array('id' => array(), 'color' => array());
        $isPrekryv = false;
        for ($i = 1; $i <= 5; $i++) {
            ?> 
            <div class="coll<?php echo $i; ?>">
                <!--___________________HLAVICKA___________________ -->
                <div class="coll_head">
                    <?php
                    switch ($i) {
                        case 1: echo "<span class='coll_text'>Pondelok <br /> " . str_replace('-', '.', $dates[0]) . "</span>";
                            break;
                        case 2: echo "<span class='coll_text'>Utorok <br /> " . str_replace('-', '.', $dates[1]) . "</span>";
                            break;
                        case 3: echo "<span class='coll_text'>Streda <br /> " . str_replace('-', '.', $dates[2]) . "</span>";
                            break;
                        case 4: echo "<span class='coll_text'>Štvrtok <br /> " . str_replace('-', '.', $dates[3]) . "</span>";
                            break;
                        case 5: echo "<span class='coll_text'>Piatok <br /> " . str_replace('-', '.', $dates[4]) . "</span>";
                            break;
                    }
                    ?>    
                </div>

                <!--___________________BUNKY___________________ -->

                <?php
                foreach ($events as $event)
                    if (($event['event_day'] == $i) OR ($event['event_day'] == changeFormatDate($dates[$i - 1]))) {
                        if ((!in_array($event['users'][0], $user_color['id'])) && (sizeof($event['users']) == 1)) {
                            $user_color['id'][$j] = $event['users'][0];
                            $user_color['color'][$j] = $colors[$j % 10];
                            $color = $user_color['color'][$j];
                            $j++;
                        } else if (sizeof($event['users']) > 1) {
                            //smart_dump($event['users']);
                            $color = 'red';
                            $isPrekryv = true;
                        } else {
                            $color = $user_color['color'][array_search($event['users'][0], $user_color['id'])];
                        }


                        echo '<a href="' . base_url() . "user/editevent/" . $event["id"] . '" title="' . $event['description'] . '">
                              <div class="cell" 
                                 style="top: ' . ((($event['start_time'] - 480) / 2) + 71) . 'px; 
                                        height: ' . (($event['end_time'] - $event['start_time']) / 2) . 'px;
                                        background-color: ' . $color . ';
                            ">
                              <div class="cell_room">' . getRoomName($event['room_id']) . '</div>
                              <div class="cell_time">' . getStringTimeFromMinutes($event['start_time']) . "-" . getStringTimeFromMinutes($event['end_time']) . '</div>
                              <div class="cell_text">' . $event['description'] . '</div>
                               </div></a>';
                    }
                ?>




            </div> <?php }; ?>     

    </div> 
    <div id="legenda">
        <?php
        echo "<div class='color-prekryv' style='clear: left; color: red;";
        if ($isPrekryv) {
            echo "display: block;'>";
        } else {
            echo "display : none;'>";
        }
        echo "<div style='margin-top: 5px; float:left; width:10px; height: 10px; background-color: red;'></div>
                   <div style='float:left;'> &nbsp;  Prekrývanie
                 </div> </div>";
        
        for ($i = 0; $i < sizeof($user_color['id']); $i++) {
            $user_legend = getUserName($user_color['id'][$i]);
            echo "<div class='color' style='clear: left; color: " . $user_color['color'][$i] . "'>
                   <div style='margin-top: 5px; float:left; width:10px; height: 10px; background-color: " . $user_color['color'][$i] . ";'></div>
                   <div style='float:left;'> &nbsp;  " . $user_legend['first_name'] . " " . $user_legend['surname'] . "</div></div>";

        }


        
        ?>
    </div> 

</div>
<div id="search">
    <?php
            foreach ($users as $user) {
                $pole_users[$user['id']] = $user['surname'] . " " . $user['first_name'];
            }
            isset($select_users) ? $selected = $select_users : $selected = "";
            foreach ($departments as $department) {
                $pole_departments[$department['department']] = $department['department'];
            }
            !empty($select_departments) ? $selected_departments = $select_departments : $selected_departments = "Všetky";
            
            echo form_open(base_url() . 'user/calendar');
            if (!isset($date)) {
                $date = "";
            }

            $date_data = array('name' => 'date',
                'class' => 'tcal',
                'value' => $date
            );

    echo '<div id="reset_search">';
        echo "<div class='search_div_left'></div>";
        echo "<div class='search_div_right'>";
        echo form_submit('reset', 'Resetovanie vyhľadávania', 'class="submit_edit" ');
        echo "</div>";
    echo '</div>';
    echo '<div id="search_date">';
        echo '<h4>Vyhľadávanie podľa dátumu</h4>';
        echo "<div class='search_div_left'>";
        echo form_label('Vyber dátum: ', 'date') . form_input($date_data);
        echo "</div>";
        echo "<div class='search_div_right'>";
        echo form_submit('submit_date', 'Načítaj údaje podľa dátumu', 'class="submit" ');
        echo "</div>";
    echo '</div>';
    echo '<div id="search_users">';
    echo '<h4>Vyhľadávanie podľa používateľov</h4>';
        if ($step == 0) {
            echo "<div class='search_div_left'>";
            echo form_label('Vyber oddelenia :  ', 'department', 'id="list_departments"') .  form_multiselect('department[]', $pole_departments, $selected_departments, "size='15' id='multiselect_d'");
            echo "<button type='button' id='button' class='submit_edit'>Zobraziť oddelenia</button>";
            echo "</div>";
            echo "<div class='search_div_right'>";
            echo form_submit('submit_department', 'Načítaj osoby z oddelení', 'class="submit" ');
            echo "</div>";
        } else if ($step == 1) {
            echo "<div class='search_div_left'>";
            echo "Vybraté oddelenia: <ul><li>" . implode('</li><li>', $selected_departments);
            echo "</li></ul></div>";
            echo "<div class='search_div_right'>";
            echo form_submit('edit_department', 'Zmeniť vybraté oddelenia', 'class="submit_edit" ');
            echo "</div>";
            echo "<div class='search_div_left'>";
            echo form_label('Vyber používateľov :', 'user') . form_multiselect('user[]', $pole_users, $selected, "size='15' id='multiselect_u'");
            echo "&nbsp;&nbsp;<button type='button' id='button2' class='submit_edit'>Zobraziť používateľov</button>";
            echo "</div>";
            echo "<div class='search_div_right'>";
            echo form_submit('submit_user', 'Načítaj údaje', 'class="submit" ');
            echo "</div>";
        } else {
            echo "<div class='search_div_left'>";
            echo "Vybraté oddelenia: <ul><li>" . implode('</li><li>', $selected_departments);
            echo "</li></ul></div>";
            echo "<div class='search_div_right'>";
            echo form_submit('edit_department', 'Zmeniť vybraté oddelenia', 'class="submit_edit" ');
            echo "</div>";
            echo "<div class='search_div_left'>";
            echo 'Vybraté osoby: <ul><li>' . implode('</li><li>', $select_users_names);
            echo "</li></ul></div>";
            echo "<div class='search_div_right'>";
            echo form_submit('edit_user', 'Zmeniť vybraté osoby', 'class="submit_edit" ');
            echo "</div>";
        }
    echo '</div>';
    echo form_close();
    
?>
    </div>    
<div id="page_bottom">
    &nbsp;
</div>
</div>
<!-- TASK AREA -->


<div class= "cTask">

</div>